package crawler.web.framework;

import javax.annotation.Resource;

import library.Tracer;
import library.database.mongo.MongoDB;
import library.thread.flow.ParameterBox;
import library.thread.flow.WorkflowContainer;

import org.springframework.stereotype.Component;

import crawler.web.beans.CrawlURL;
import crawler.web.engine.config.ConfigBatchInit;
import crawler.web.engine.config.SpBN;
import crawler.web.engine.custom.TwoDBFrontier;
import crawler.web.engine.custom.URLFilter;

/**
 * @author waynechen Feb 8, 2012 11:23:46 PM
 */
@Component(value = "CrawlerContainer")
public class CrawlerContainer extends WorkflowContainer<Crawler> {
	public static transient volatile int NO = 1;

	public static String getNO() {
		return "crawler." + NO++;
	}

	public CrawlerContainer() throws SecurityException, NoSuchMethodException {
		super("CrawlerContainer");
	}

	@Resource(name = SpBN.bean_url_frontier)
	private TwoDBFrontier twoDBFrontier;

	@Resource(name = SpBN.bean_MongoDB)
	private MongoDB mongo;

	@Resource(name = "URLFilter")
	private URLFilter urlFilter;

	@Resource(name = SpBN.conf_htmlTabName)
	private String tableContext0;

	@Resource(name = SpBN.conf_parseTabName1)
	private String tableContext1;

	@Resource(name = SpBN.conf_parseTabName2)
	private String tableContext2;

	@Override
	protected void preExecute(ParameterBox args) {
		Tracer.info("\n-------爬虫容器启动--------------\n");
		try {
			//--------------------爬虫初始化-------------------
			//给frontier初始化
			for (CrawlURL url : ConfigBatchInit.getCrawlURLList())
				if (urlFilter.apply(null, url))
					twoDBFrontier.put(url);

			//--------------------分析器1初始化-------------------
			mongo.createIndex(tableContext0, "parseStatus");
			mongo.createIndex(tableContext0, "url.oriUrl");

			//--------------------分析器2初始化-------------------
			//设置索引
			mongo.createIndex(tableContext1, "fingerPrint");
			mongo.createIndex(tableContext1, "navigate");
			mongo.createIndex(tableContext1, "title");
			mongo.createIndex(tableContext1, "parseStatus");
			mongo.createIndex(tableContext1, "src");
			mongo.createIndex(tableContext1, "perfectionScore");
			mongo.createIndex(tableContext1, "pageType");
			mongo.createIndex(tableContext1, "location.identity");
			mongo.createIndex(tableContext1, "url.oriUrl");

			//--------------------分析器3初始化-------------------
			//设置索引
			mongo.createIndex(tableContext2, "fingerPrint");
			mongo.createIndex(tableContext2, "navigate");
			mongo.createIndex(tableContext2, "title");
			mongo.createIndex(tableContext2, "parseStatus");
			mongo.createIndex(tableContext2, "src");
			mongo.createIndex(tableContext2, "perfectionScore");
			mongo.createIndex(tableContext2, "pageType");
			mongo.createIndex(tableContext2, "location.identity");
		} catch (Exception e) {
			set();
			Tracer.exception(e);
			Tracer.exception("异常...停止所有爬虫......");
		}
	}

	@Override
	protected void postExecute(ParameterBox args) {
		Tracer.info("\n-------爬虫容器结束--------------\n");
	}
}

//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
